package com.rla.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.common.enums.BusinessType;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.domain.vo.VipUserAddressVo;
import com.rla.domain.bo.VipUserAddressBo;
import com.rla.service.IVipUserAddressService;
import com.rla.common.core.page.TableDataInfo;

/**
 * 用户收货地址
 *
 * @author rla
 * @date 2025-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/vip/user/address")
public class VipUserAddressController extends BaseController {

    private final IVipUserAddressService iVipUserAddressService;

    /**
     * 查询用户收货地址列表
     */
    @GetMapping("/list")
    public TableDataInfo<VipUserAddressVo> list(VipUserAddressBo bo, PageQuery pageQuery) {
        return iVipUserAddressService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户收货地址列表
     */
    @Log(title = "用户收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VipUserAddressBo bo, HttpServletResponse response) {
        List<VipUserAddressVo> list = iVipUserAddressService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户收货地址", VipUserAddressVo.class, response);
    }

    /**
     * 获取用户收货地址详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<VipUserAddressVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iVipUserAddressService.queryById(id));
    }

    /**
     * 新增用户收货地址
     */
    @Log(title = "用户收货地址", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VipUserAddressBo bo) {
        return toAjax(iVipUserAddressService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户收货地址
     */
    @Log(title = "用户收货地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VipUserAddressBo bo) {
        return toAjax(iVipUserAddressService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户收货地址
     *
     * @param ids 主键串
     */
    @Log(title = "用户收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iVipUserAddressService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    /**
     * 修改默认收货地址
     */
    @Log(title = "修改默认收货地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/default")
    public R<Void> editDefault(@Validated(EditGroup.class) @RequestBody VipUserAddressBo bo) {
        return toAjax(iVipUserAddressService.editDefault(bo) ? 1 : 0);
    }
}
